//
//  BaseViewController.m
//  Less_Study
//
//  Created by Lj on 2016/10/23.
//  Copyright © 2016年 Lj. All rights reserved.
//

#define BaseViewController_BASE_TAG 1024

#import "BaseViewController.h"
#import "UIBarButtonItem+SGBarButton.h"
#import "RecordObject.h"

@interface BaseViewController () <UINavigationControllerDelegate>

@end

@implementation BaseViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSString *str = [NSString stringWithFormat:@"this class name:%@",NSStringFromClass([self class])];
    
    DLog(@"%@", str);
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
//    if (self.navigationController.viewControllers.count == 1) {
//        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
//    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
//    self.navigationController.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = Main_BackColor;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
//    CGRect rect = CGRectMake(0, 0, App_Frame_Width, BHLine_Height);
//    UIGraphicsBeginImageContext(rect.size);
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    CGContextSetFillColorWithColor(context, Dividing_Line_Color.CGColor);
//    CGContextFillRect(context, rect);
//    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    [self.navigationController.navigationBar setShadowImage:img];
//    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];
    
//    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
//    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    
    [BHSettingUtil addTapGestureInView:self.view addTarget:self action:@selector(hideKeyBoardClick) withTag:0];
}

- (UIImage *)imageWithFrame:(CGRect)frame alphe:(CGFloat)alphe {
    frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
    UIColor *redColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:alphe];
    UIGraphicsBeginImageContext(frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [redColor CGColor]);
    CGContextFillRect(context, frame);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

#pragma mark - 外部调用方法
// 自定义导航栏
-(void)initNavBar:(NSString *)title {
    self.navigationController.navigationBarHidden = YES;
    _navBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, App_Frame_Width, kNavbarAndStatusBar)];
//    _navBar.backgroundColor=[UIColor whiteColor];
    _navBar.backgroundColor = Bg_Color_F9F9F9;
    _navBar.tag = BaseViewController_BASE_TAG;
    [self.view addSubview:_navBar];
    
    UIImageView *backImgView=[[UIImageView alloc]initWithFrame:CGRectMake(0, kStatusHeight, App_Frame_Width, kTopBarHeight)];
    backImgView.image=[UIImage imageNamed:@"icon-fanhui-54"];
    backImgView.userInteractionEnabled=YES;
    backImgView.tag = BaseViewController_BASE_TAG+1;
    [_navBar addSubview:backImgView];
    
    UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, kStatusHeight, App_Frame_Width, kTopBarHeight)];
    titleLabel.backgroundColor = Bg_Color_F9F9F9;
//       UILabel *titleLabel=[[UILabel alloc]init];
//    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.text=title;
    titleLabel.font = BHFont_Size_17;
    titleLabel.textColor=Text_Color_333333;
    titleLabel.tag = BaseViewController_BASE_TAG+2;
    [_navBar addSubview:titleLabel];
}

-(void)initLoginNavBar:(NSString *)title {
    self.navigationController.navigationBarHidden = YES;
    _navBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, App_Frame_Width, kNavbarAndStatusBar)];
    _navBar.backgroundColor = Bg_Color_F9F9F9;
//    _navBar.backgroundColor = [UIColor whiteColor];
    _navBar.tag = BaseViewController_BASE_TAG;
    [self.view addSubview:_navBar];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setFrame:CGRectMake(0, kStatusHeight, 50, 44)];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:@"home-7"] forState:UIControlStateNormal];
    [_navBar addSubview:backButton];
    
    UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, kStatusHeight, App_Frame_Width, kTopBarHeight)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.text=title;
    titleLabel.font = BHFont_Size_17;
    titleLabel.textColor=Text_Color_333333;
    titleLabel.tag = BaseViewController_BASE_TAG+2;
    [_navBar addSubview:titleLabel];
}

//- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
//    if (self.navigationController.viewControllers.count == 1) {
//        return NO;
//    }else {
//        return YES;
//    }
//}


// 系统导航栏返回按钮
- (void)initNavBarBackBtn {
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    UIBarButtonItem *leftItem = [UIBarButtonItem createBackBarButtonForDelegate:self action:@selector(backAction:)];
    self.navigationItem.leftBarButtonItem = leftItem;
}

// 将tabbar隐藏的push
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    viewController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:viewController animated:animated];
    //修改tabBar的frame
    CGRect frame = self.tabBarController.tabBar.frame;
    frame.origin.y = App_Frame_Height - frame.size.height;
    self.tabBarController.tabBar.frame = frame;
}

//指定返回几个页面
- (void)popViewController:(UIViewController *)viewController index:(NSInteger)index {
    NSInteger indexConunt = [[self.navigationController viewControllers]indexOfObject:self];
    [viewController.navigationController setNavigationBarHidden:NO animated:NO];
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:indexConunt - MIN(indexConunt, index)] animated:YES];
}

// 系统导航栏设置右侧按钮
- (void)setRightBarButtonItem:(UIBarButtonItem *)_rightBarButtonItem {
    UIBarButtonItem *spaceButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spaceButtonItem.width = -5;
    if (_rightBarButtonItem) {
        [self.navigationItem setRightBarButtonItems:@[spaceButtonItem,_rightBarButtonItem]];
    }else {
        [self.navigationItem setRightBarButtonItems:@[spaceButtonItem]];
    }
}
// 系统导航栏设置左侧按钮
- (void)setLeftBarButtonItem:(UIBarButtonItem *)_leftBarButtonItem {
    UIBarButtonItem *spaceButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spaceButtonItem.width = -5;
    if (_leftBarButtonItem) {
        [self.navigationItem setLeftBarButtonItems:@[spaceButtonItem,_leftBarButtonItem]];
    }else {
        [self.navigationItem setLeftBarButtonItems:@[spaceButtonItem]];
    }
    
}

#pragma mark - btn action
- (void)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - hide keyboard
- (void)hideKeyBoardClick {
    if (self.touchEnable) {
        [self.view endEditing:YES];
    }
}

- (BOOL)isLogin {
    SGUserModel *userModel = [SGUserInfo getUserInfo];
    if ([BHSettingUtil dataAndStringIsNull:userModel.Token]) {
        [(AppDelegate *)[UIApplication sharedApplication].delegate gotoLogin:self];
        return NO;
    }else {
        return YES;
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
